<template>
	<view class="main">
		<ul class="list">
			<li>
				<p>微信支付</p>
				<u-radio-group class="sele" v-model="radiovalue1" placement="column" @change="groupChange">
					<u-radio name="1"></u-radio>
				</u-radio-group>
			</li>
			<!-- <li>
				<p>支付宝支付</p>
				<u-radio-group class="sele" v-model="radiovalue1" placement="column" @change="groupChange">
					<u-radio name="2"></u-radio>
				</u-radio-group>
			</li> -->
		</ul>
		<view class="bottom">
			<view class="in">
				<view class="price">￥{{ price }}</view>
				<view class="btn" v-if="!ispay">订单支付</view>
				<view class="btn" v-else @tap="pay">订单支付</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		payNow,
		checkOrder
	} from '@/api/index.js'
	export default {
		data() {
			return {
				ispay: true,
				radiovalue1: '1',
				paytype: 'wechat',
				tag: '',
				gopay: false,
				price: ''
			};
		},
		onLoad(e) {
			this.tag = e.tag;
			this.price = e.price
		},
		// onShow() {
		// 	if (this.gopay) {
		// 		checkOrder({
		// 			order_num: this.tag
		// 		}).then(res => {
		// 			if (res.data.status == 0) {
		// 				this.ispay = true
		// 				uni.showToast({
		// 					title: '支付失败',
		// 					icon: 'none'
		// 				})
		// 				this.gopay = false
		// 			} else {
		// 				this.ispay = false
		// 				uni.showToast({
		// 					title: '支付成功',
		// 					icon: 'none'
		// 				})
		// 				setTimeout(() => {
		// 					uni.reLaunch({
		// 						url: '/pages/index/index'
		// 					})
		// 				}, 1000)
		// 			}
		// 		})
		// 	}
		// },
		onShow() {
					let options = uni.getEnterOptionsSync();
					console.log(options);
					if (options.scene == '1038' &&
						options.referrerInfo.appId == 'wxef277996acc166c3') {
						// 代表从收银台小程序返回
						let extraData = options.referrerInfo.extraData;
						if (!extraData) {
							// "当前通过物理按键返回，未接收到返参，建议自行查询交易结果";
							console.log(extraData, 11111);
						} else {
							if (extraData.code == 'success') {
								// "支付成功";
								setTimeout(() => {
									uni.navigateTo({
										url: '/pageD/orderList'
									})
								}, 1500)
								console.log(extraData, 222222222);
							} else if (extraData.code == 'cancel') {
								uni.$u.toast('支付取消')
								uni.navigateTo({
									url: '/pageD/orderList'
								})
								// "支付已取消";
								// uni.redirectTo({
								// 	url: '/pages/Purchase-order/Purchase-order'
								// })
								// console.log('fail:' + JSON.stringify(err));
							} else {
								// "支付失败：" + extraData.errmsg;
								uni.$u.toast('支付失败')
								uni.navigateTo({
									url: '/pageD/orderList'
								})
								// console.log(extraData, 444444);
							}
						}
					}
				},
		methods: {
			async pay() {
				let res = await payNow({
					order_num: this.tag,
					paytype: this.paytype
				})
				console.log(res)
				if(res.code == 1){
					this.extraData = res.data
					// this.gopay = true
				}else{
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
					return
				}
				if (this.radiovalue1 == 1) {
					uni.openEmbeddedMiniProgram({
						appId: 'wxef277996acc166c3',
						extraData: {
							cusid: res.data.cusid,
							appid: res.data.appid,
							version: res.data.version,
							trxamt: res.data.trxamt,
							reqsn: res.data.reqsn,
							notify_url: res.data.notify_url,
							body: res.data.body,
							randomstr: res.data.randomstr,
							sign: res.data.sign,
							signtype: res.data.signtype,
							paytype: res.data.paytype
						}
					})
				} else {
					
				}
			},
			groupChange(e) {
				if (e == 1) {
					this.paytype = 'wechat'
				} else {
					this.paytype = 'alipay'
				}
			},
		}
	};
</script>

<style lang="scss" scoped>
	.main {
		min-height: calc(100vh - 44px);
		background-color: #f6f6f6;
		padding: 15px;

		.list {
			li {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 18px 18rpx 18px 20px;
				background-color: #fff;
				border-radius: 6px;
				margin-bottom: 12px;
				font-size: 14px;
				color: #3d3d3d;
				position: relative;

				image {
					width: 40px;
					height: 40px;
					margin-right: 15px;
				}

				// .sele {
				// 	position: absolute;
				// 	right: 15px;
				// }
			}
		}

		.bottom {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100vw;
			background-color: #fff;

			.in {
				padding: 10px 15px;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.price {
					color: #000;
					font-size: 20px;
					font-weight: 700;
				}

				.btn {
					font-size: 12px;
					padding: 10px 24px;
					background-color: #ff6347;
					border-radius: 100px;
					color: #fff;
				}
			}
		}
	}
	
	::v-deep .u-radio-group {
		flex: 0 !important;
	}
</style>